(function (vc) {
    vc.extends({
        data: {
            addProductLabelInfo: {
                labelCd: '',
                productId: '',
                labels: [],
            }
        },
        _initMethod: function () {
            $that._listLabels();
        },
        _initEvent: function () {
            vc.on('addProductLabel', 'openAddLabelModal', function (_product) {
                $('#addProductLabelModel').modal('show');
                $that.addProductLabelInfo.productId = _product.productId;
            });
        },
        methods: {
            addProductLabelValidate() {
                return vc.validate.validate({
                    addProductLabelInfo: $that.addProductLabelInfo
                }, {
                    'addProductLabelInfo.labelCd': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "标签不能为空"
                        }
                    ],
                    'addProductLabelInfo.productId': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "产品不能为空"
                        }
                    ]
                });
            },
            saveProductLabelInfo: function () {
                if (!$that.addProductLabelValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                $that.addProductLabelInfo.shopId = vc.getCurrentCommunity().shopId;
                vc.http.apiPost(
                    '/product/saveProductLabel',
                    JSON.stringify($that.addProductLabelInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#addProductLabelModel').modal('hide');
                            $that.clearAddLabelInfo();
                            vc.jumpToPage('/#/pages/goods/productLabelManage?productId=' + $that.addProductLabelInfo.productId);
                            vc.toast(_json.msg);
                            return;
                        } else if(_json.code == 404){
                            vc.toast(_json.msg);
                            return;
                        }
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            clearAddLabelInfo: function () {
                $that.addProductLabelInfo = {
                    labelCd: '',
                    productId: '',
                    labels: [],
                };
            },
            _listLabels: function () {
                var param = {
                    params: {
                        page: 1,
                        row: 100,
                        shopId: vc.getCurrentCommunity().shopId,
                        hasSystem: 'Y'
                    }
                };
                //发送get请求
                vc.http.apiGet('/label/queryLabel',
                    param,
                    function (json, res) {
                        let _labelManageInfo = JSON.parse(json);
                        $that.addProductLabelInfo.labels = _labelManageInfo.data;
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
        }
    });
})(window.vc);
